<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-date"></i> 表单</el-breadcrumb-item>
                <el-breadcrumb-item>编辑器</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="plugins-tips">
            Vue-Quill-Editor：基于Quill、适用于Vue2的富文本编辑器。
            访问地址：<a href="https://github.com/surmon-china/vue-quill-editor" target="_blank">vue-quill-editor</a>
        </div>
        <el-upload
                v-show="false"
                class="avatar-uploader"
                action="http://upload-z2.qiniup.com/"
                :data='fileUpload'
                :show-file-list="false"
                :on-success="uploadSuccess"
                :before-upload="beforeUpload">
        </el-upload>
        <quill-editor
                ref="myQuillEditor"
                v-model="content"
                :options="editorOption"
        >
        </quill-editor>
        <el-button class="editor-btn" type="primary" @click="submit">提交</el-button>
    </div>
</template>

<script>
    import { quillEditor } from 'vue-quill-editor';
    import { getUpTokenAjax } from '../../api/api'
    export default {
        data: function(){
            return {
                fileUpload:{
                    token:'',
                    key:''
                },
                content: '',
                editorOption: {
                    placeholder: '请输入内容',
                    theme: 'snow',
                    modules: {
                        toolbar: {
                            container: [
                                ['bold', 'italic', 'underline', 'strike'],
                                ['blockquote', 'code-block'],
                                [{'header': 1}, {'header': 2}],
                                [{'list': 'ordered'}, {'list': 'bullet'}],
                                [{'script': 'sub'}, {'script': 'super'}],
                                [{'indent': '-1'}, {'indent': '+1'}],
                                [{'direction': 'rtl'}],
                                [{'size': ['small', false, 'large', 'huge']}],
                                [{'header': [1, 2, 3, 4, 5, 6, false]}],
                                [{'color': []}, {'background': []}],
                                [{'font': []}],
                                [{'align': []}],
                                ['link', 'image', 'video'],
                                ['clean']],
                            handlers: {
                                'image': function (value) {
                                    if (value) {
                                        document.querySelector('.avatar-uploader input').click()
                                    } else {
                                        this.quill.format('image', false);
                                    }
                                }
                            }
                        }
                    }

                },
                imgUrl:'',
            }
        },
        mounted(){
            getUpTokenAjax().then(res=>{
                this.fileUpload.token = res.data.token
            })
        },
        components: {
            quillEditor
        },
        methods: {
            onEditorChange({ editor, html, text }) {
                this.content = html;
            },
            submit(){
                console.log(this.content);
                this.$message.success('提交成功！');
            },
            uploadSuccess(res, file){
                this.imgUrl = 'http://meeting-resource.changlujia.com/'+file.name

                // 首先获取富文本编辑器的实例
                let quill = this.$refs.myQuillEditor.quill
                // 上传成功所执行
                if (res) {
                    // 获取光标所在位置
                    let length = quill.getSelection().index;
                    // 插入图片res为服务器返回的数据
                    quill.insertEmbed(length, 'image', this.imgUrl)
                    // 光标移动至文本末端
                    quill.setSelection(length + 1)
                } else {
                    this.$message.error('图片插入失败')
                }
            },
            beforeUpload(file){
                this.fileUpload.key = file.name
            },
        },
    }
</script>
<style scoped>
    .editor-btn{
        margin-top: 20px;
    }
</style>
